package com.gulimall.product.service.impl;

import com.gulimall.product.entity.AttrEntity;
import com.gulimall.product.service.AttrService;
import com.gulimall.product.service.CategoryService;
import com.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.Query;

import com.gulimall.product.dao.AttrGroupDao;
import com.gulimall.product.entity.AttrGroupEntity;
import com.gulimall.product.service.AttrGroupService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrService attrService;
    @Autowired
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params,Long catelogId) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)){
            queryWrapper.and((obj)->{
                obj.like("attr_group_name",key).or().like("descript",key);
            });
        }
        //如果传的值为0 查询所有
        if (catelogId.equals(0L)){
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }else {
            //拼接查询条件
            queryWrapper.eq("catelog_id",catelogId);

            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }
    }

    @Override
    public AttrGroupEntity getAttrGroupById(Long attrGroupId) {
        //根据id查询属性分组
        AttrGroupEntity attrGroupEntity = baseMapper.selectById(attrGroupId);

        //根据查询到的属性分组查询分类的父子孙id
        Long catelogId = attrGroupEntity.getCatelogId();
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);

        attrGroupEntity.setCatelogPath(catelogPath);

        return attrGroupEntity;
    }

    @Transactional
    @Override
    public void updateDetaile(AttrGroupEntity attrGroup) {
        //保证其他表中的冗余字段同步更新
        //先将自己表中的数据进行更新
        this.updateById(attrGroup);

        //修改引用表中的数据 {attr}
        if (StringUtils.isNotEmpty(attrGroup.getAttrGroupName())){
            attrService.updateAttrGroup(attrGroup.getAttrGroupId(),attrGroup.getAttrGroupName());
        }
    }

    /**
     * 根据分类id获取分类下所有分组&关联属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //根据catelogId查询所有的分组
        List<AttrGroupEntity> attrGroupEntities = this.list(
                new QueryWrapper<AttrGroupEntity>() {{
                    eq("catelog_id", catelogId);
                }}
        );

        //根据查询到的所有分组查询所有分组下的属性
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupEntities.stream().map((group)->{
            //创建接收数据的对象
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            //先将每个group相应的值copy到接收数据的对象中
            BeanUtils.copyProperties(group,attrGroupWithAttrsVo);

            //再根据attrGroupId去查询该分组下的所有属性
            List<AttrEntity> attrRelation = attrService.getAttrRelation(attrGroupWithAttrsVo.getAttrGroupId());
            //将查询到的所有属性通过set方法赋值到上面创建的对象Vo中
            attrGroupWithAttrsVo.setAttrs(attrRelation);

            //最后将这个对象返回，收集成一个集合返回
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());

        return attrGroupWithAttrsVos;
    }
}